package com.example.communitypro.util;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class GetAllUrl {
    WebApplicationContext applicationContext = (WebApplicationContext) SpringApplicationContext.getApplicationContext();

    public List<String> getAllUrl(){
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        //获取url与类和方法的对应信息
        Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
        List<String> urls = new ArrayList<>();
        for (RequestMappingInfo info : map.keySet()){
            //获取url的Set集合，一个方法可能对应多个url
            Set<String> patterns = info.getPatternsCondition().getPatterns();

            // 这里可获取请求方式 Get,Post等等
//          Set<RequestMethod> methods = info.getMethodsCondition().getMethods();
            for (String url : patterns){
                urls.add(url);
            }
        }
        return urls;
    }

    public static boolean isSuccess(HttpServletRequest request){
        String requestURI = request.getRequestURI();
        boolean res =requestURI.contains("toAdminIndex") ||requestURI.contains("communityIndex")||requestURI.contains("reportReasonIndex")||requestURI.contains("toAdminWelcome")
                ||requestURI.contains("toCommunityInfoIndex")||requestURI.contains("toMenuIndex")||requestURI.contains("toPostIndex")||requestURI.contains("toUserInfoIndex")
                ||requestURI.contains("toReportIndex") ||requestURI.contains("toSystemSetIndex")||requestURI.contains("toLoginLogIndex")
                ||requestURI.contains("toCommunityAdd")||requestURI.contains("toCommunityEdit")||requestURI.contains("toPostEdit");
        if (res){
            return true;
        }else {
            return false;
        }

    }
}
